package cn.net.mugui.net.task;

import com.alibaba.fastjson.JSONObject;
import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;
import com.mugui.base.client.net.auto.AutoTask;
import com.mugui.base.client.net.baghandle.NetHandle;
import com.mugui.base.client.net.bagsend.NetCall.Call;
import com.mugui.base.client.net.base.Task;
import com.mugui.base.client.net.bean.Message;
import com.mugui.base.client.net.bean.NetBag;
import com.mugui.bean.JsonBean;

import cn.net.mugui.net.p2p.Server;
import cn.net.mugui.net.p2p.ServerAttr;
import cn.net.mugui.net.p2p.ServerManager;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 当链接到一个新的主服务器后，与主服务器之间建立双方属性.<br>
 * 属性处理任务 <br>
 * 1.判断自身在主服务方向的dns服务器属性<br>
 * 2.从主服务同步可连通的用户列表<br>
 * 3.与返回的用户列表建立点对点链接<br>
 * 
 * @author pec
 *
 */
@Component
@Task
@AutoTask
public class PerHandleTask extends TaskCycleImpl<cn.net.mugui.net.task.PerHandleTask.PerHandleType> {

	@Override
	protected void handle(cn.net.mugui.net.task.PerHandleTask.PerHandleType poll) {

		switch (poll.type) {
		case 1:
			getServerPer(poll.serverAttr);
			break;
		case 2:
			putSelfPer(poll.serverAttr);
			break;
		case 3:
			isP2PConnected(poll);
			break;
		default:
			break;
		}

	}

	/**
	 * 得到地址信息
	 * 
	 * @param poll.getPer(),
	 */
	private void isP2PConnected(PerHandleType poll) {
		server.send("per.method.isP2PConnected", poll.getPer(), poll.getServerAttr().getAddress()).son(new Call() {

			@Override
			public Message ok(Message data) {
				System.out.println("per.method.getAddressInfo" + data);
				return Message.ok();
			}

			@Override
			public Message err(Message data) {
				System.out.println("per.method.getAddressInfo" + data);
				return Message.ok();
			}
		});
	}

	/**
	 * 提交并测试自身属性
	 * 
	 * @param serverAttr
	 */
	private void putSelfPer(ServerAttr serverAttr) {
		server.sendTest("per.method.putSelfPer", null, serverAttr.getAddress()).son(new Call() {

			@Override
			public Message ok(Message data) {
				System.out.println("per.method.putSelfPer" + data);
				return Message.ok();
			}

			@Override
			public Message err(Message data) {
				System.out.println("per.method.putSelfPer" + data);
				return Message.ok();
			}
		});
	}

	@Autowired
	private Server server;

	/**
	 * 得到服务器属性
	 * 
	 * @param serverAttr
	 */
	private void getServerPer(ServerAttr serverAttr) {
		NetBag netBag = new NetBag();
		netBag.setFunc("per.method.server_per");
		server.send(netBag, serverAttr.getAddress()).son(new Call() {

			@Override
			public Message ok(Message data) {
				System.out.println("per.method.server_per->" + data);
				JSONObject object = JSONObject.parseObject(data.getDate().toString());
				String string = object.getString("name");
				if (string != null) {
					serverAttr.setName(string);
				}
				serverAttr.setCheck_port(object.getInteger("check_port"));
				NetBag netBag2 = NetHandle.netBagLocal.get();
				String session = netBag2.getSession();

				if (!serverAttr.getAddress().equals(netBag2.getSession())) {
					serverAttr.setAddress(session);
					serverManager.del(session);
					serverManager.add(session, serverAttr);
					return Message.ok();
				}
//				serverAttr.setPort(object.getInteger("port"));
				add(new PerHandleType().setServerAttr(serverAttr).setType(2));
				return Message.ok();
			}

			@Override
			public Message err(Message data) {
				System.out.println("per.method.server_per->" + data);
				return Message.ok();
			}
		});
	}

	@Getter
	@Setter
	@Accessors(chain = true)
	public static class PerHandleType extends JsonBean {

		/**
		 * 
		 */
		private static final long serialVersionUID = -3646882467205060483L;

		ServerAttr serverAttr;

		Object per;

		/**
		 * 1.得到服务器属性<br>
		 * 2.向服务器提交并测试自身属性<br>
		 * 3.向对方询问此地址是否可p2p连接<br>
		 */
		int type;

	}

	@Autowired
	private ServerManager serverManager;

	public void add(String key) {
		ServerAttr serverAttr = serverManager.get(key);
		add(new PerHandleType().setServerAttr(serverAttr).setType(1));
//		add(new PerHandleType().setServerAttr(serverAttr).setType(3));
	}

}
